package com.hmall.controller.globalexception;

import com.hdmall.business.common.common.ServerResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import javax.servlet.http.HttpServletRequest;

/**
 * 处理token失效
 * 全局异常处理
 */
@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
    /**
     * 在controller里面内容执行之前，校验一些参数不匹配啊，Get post方法不对啊之类的
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ServerResponse jsonHandler(HttpServletRequest request, Exception e) throws Exception {
        if ("无该用户,请重新登录".equals(e.getMessage())){
            return  ServerResponse.createByStatus(11,e.getMessage());
        }
        if ("用户已失效,请重新登录".equals(e.getMessage())){
            return  ServerResponse.createByStatus(12,e.getMessage());
        }
        if ("找不到配送方式".equals(e.getMessage())){
            return  ServerResponse.createByStatus(51,e.getMessage());
        }
        return  ServerResponse.createByStatus(500,e.getMessage());
    }
}
